package com.wing.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.common.util.GUID;
import com.wing.sys.dao.AuthDao;
import com.wing.sys.domain.Auth;
/**
 * 
 * @author djl
 * 
 */
@Service
@Transactional
public class AuthManager {
	@Autowired
	private AuthDao rightDao;

	public void insert(Auth r) {
		rightDao.insert(r);
	}

	public void update(Auth r) {
		rightDao.update(r);
	}

	public List<Auth> getAuthsByTypeResId(String type, String resId) {
		return rightDao.getAuthsByTypeResId(type,resId);
	}

	public List getAuthsByTypeOwnerId(String type, String ownerId) {
		return rightDao.getAuthsByTypeOwnerId(type,ownerId);
	}

	public boolean isHasAuth(String type, String ownerId,String resId){
		if(ownerId == null) return  false;
		List<Auth> owerList = getAuthsByTypeResId(type,resId);
		for(int i = 0; i < owerList.size();i++){
			if(ownerId.equals(owerList.get(i).getOwner_id())) return true;
		}
		return  false;
	}
	
	public List getAuths(String type, String ownerId,String resId){
		return rightDao.getAuthsByTypeOwnerIdResId(type, ownerId,resId);
	}
	/**
	 * 根据type ownerId更新 auth
	 * 根据type ownerId删除所有的auth 并添加type owner resids。
	 * @param type
	 * @param ownerId
	 * @param resIds
	 */
	public void updateAuthsByTypeOwnerId(String type, String ownerId,
			String[] resIds) {
		rightDao.deleteAuthsByTypeOwnerId(type,ownerId);
		for(int i=0;i<resIds.length;i++){
			if(!resIds[i].trim().equals("")){
				Auth a = new Auth();
				a.setId(new GUID().toString());
				a.setOwner_id(ownerId);
				a.setRes_id(resIds[i]);
				a.setType(type);
				rightDao.insert(a);
			}
		}
		
	}
	

	public void deleteAuthsByTypeOwnerId(String type, String ownerId) {
		rightDao.deleteAuthsByTypeOwnerId(type,ownerId);
	}
	
	public void deleteAuthsByTypeResId(String type, String resId) {
		rightDao.deleteAuthsByTypeResId(type,resId);
	}

	public void deleteAuth(Auth auth) {
		rightDao.delete(auth.getId());
	}
	
	
}



